import { createRouter, createWebHistory } from 'vue-router'
import store from "@/store";
import{notification} from "ant-design-vue";
import PaymentPage from '@/views/main/PaymentPage.vue';

const routes = [
  {
    path: '/login',
    name:'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/',
    name: 'main',
    component: () => import('../views/main.vue'),
    meta: {
      loginRequire: true
    },
    children: [{
      path: "welcome",
      component: () => import('../views/main/welcome.vue'),
    }, {
      path: 'passenger',
      component: () => import('../views/main/passenger.vue'),
    }, {
      path: 'ticket',
      component: () => import('../views/main/daily-train-ticket.vue'),
    }, {
      path: 'order',
      component: () => import('../views/main/order_demo.vue'),
    }, {
      path: 'myTicket',
      component: () => import('../views/main/ticket.vue'),
    }]
  }, {
    path: '',
    redirect: '/welcome'
  },
    {
        path:'/payment',
        name:'Payment',
        component:PaymentPage
    },{
        path:'/order-success',
        name:'OrderSuccess',
        component:()=>import('../views/main/OrderSuccess.vue')
    }];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next)=> {
  if (to.matched.some(function (item) {
    console.log(item, "昊昊不确定是否需要登录校验：", item.meta.loginRequire || false);
    return item.meta.loginRequire
  })) {
    const _member = store.state.member;
    console.log("页面登录校验开始：", _member);
    if(!_member.token){
      console.log("用户未登录或登录超时",_member);
      notification.error({description: "未登录或者登录超时"});
      next('/login');
    } else {
      next();
    }
  }else{
    next();
  }
});
export default router
